package condominio.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import condominio.excepcion.DAOExcepcion;
import condominio.modelo.Residente;
import condominio.util.ConexionBD;

public class ResidenteDAO extends BaseDAO {

	public Residente insertar(Residente vo) throws DAOExcepcion {
		//System.out.println("ResidenteDAO: insertar()");
		System.out.println("ResidenteDAO: Residente Insertado");

		String query = "INSERT INTO residente(dniRESIDENTE, nameRESIDENTE, correoRESIDENTE, passRESIDENTE) VALUES (?,?,?,?)";
		
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			//stmt.setInt(1, vo.getIdResidente());
			stmt.setString(1, vo.getDniResidente());
			stmt.setString(2, vo.getNameResidente());
			stmt.setString(3, vo.getCorreoResidente());
			stmt.setString(4, vo.getPassResidente());
			
			
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo insertar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}
	
	public Residente actualizar(Residente vo) throws DAOExcepcion {
		String query = "update residente set dniRESIDENTE=?,nameRESIDENTE=?,correoRESIDENTE=?,passRESIDENTE=? where idResidente=?";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, vo.getDniResidente());
			stmt.setString(2, vo.getNameResidente());
			stmt.setString(3, vo.getCorreoResidente());
			stmt.setString(4, vo.getPassResidente());
			stmt.setInt(5, vo.getIdResidente());
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo actualizar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}
	
	public Collection<Residente> listar(String Nombre) throws DAOExcepcion {
		Collection<Residente> c = new ArrayList<Residente>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			String query = "select idResidente,dniResidente,nameResidente,correoResidente,passResidente from residente where nameResidente like '%" + Nombre + "%' order by IDResidente";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			while (rs.next()) {
				Residente vo = new Residente();
				vo.setIdResidente(rs.getInt("idResidente"));
				vo.setDniResidente(rs.getString("dniResidente"));
				vo.setNameResidente(rs.getString("nameResidente"));
				vo.setCorreoResidente(rs.getString("correoResidente"));
				vo.setPassResidente(rs.getString("passResidente"));
				c.add(vo);
			}

		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return c;
	}

	public void eliminar(String idResidente) throws DAOExcepcion {
		String query = "delete from residente WHERE idresidente=?";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, idResidente);
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo eliminar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
	}

}
